﻿using System;
using System.Globalization;
using System.Windows.Data;
using SystemConvert = System.Convert;

namespace Red.Controls.Converters
{
    public sealed class IntToStringConverter : IValueConverter
    {
        #region IValueConverter implementation

        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            int result = 0;
            if ( value is int )
            {
                result = (int)value;
            }
            return result.ToString();
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            int result = 0;
            if ( value is string )
            {
                result = SystemConvert.ToInt32( value );
            }
            return result;
        }
        #endregion IValueConverter implementation
    }

    public sealed class StringToIntConverter : IValueConverter
    {
        public IntToStringConverter IntToStringConverter { get; private set; } = new IntToStringConverter();

        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            return IntToStringConverter.ConvertBack( value, targetType, parameter, culture );
        }
        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            return IntToStringConverter.Convert( value, targetType, parameter, culture );
        }
    }
}
